package com.ushio.gougouwangguo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ushio.commonmodule.utils.JwtUtil;
import com.ushio.gougouwangguo.entity.mysql.ActionConfig;
import com.ushio.gougouwangguo.entity.mysql.DogCharacter;
import com.ushio.gougouwangguo.entity.mysql.UserMaterials;
import com.ushio.gougouwangguo.entity.mysql.UserTasks;
import com.ushio.gougouwangguo.entity.qo.ActionConfigQo;
import com.ushio.gougouwangguo.entity.qo.ActionConfigUpdataQo;
import com.ushio.gougouwangguo.entity.qo.UserTasksQo;
import com.ushio.gougouwangguo.entity.vo.ActionConfigVo;
import com.ushio.gougouwangguo.entity.vo.UserTasksVo;
import com.ushio.gougouwangguo.mapper.*;
import com.ushio.gougouwangguo.service.IActionConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 张文凯
 * @since 2025-09-22
 */
@Service
public class ActionConfigServiceImpl extends ServiceImpl<ActionConfigMapper, ActionConfig> implements IActionConfigService {


    @Autowired
    ActionConfigMapper actionConfigMapper;
    @Autowired
    DogCharacterMapper dogCharacterMapper;
    @Autowired
    UserMaterialsMapper userMaterialsMapper;
    @Autowired
    UserTasksServiceImpl userTasksService;
    @Autowired
    UserTasksMapper userTasksMapper;
    @Override
    public List<ActionConfigVo> getActionConfig(ActionConfigQo actionConfigQo) {
        LocalDateTime now = LocalDateTime.now();
        //获得userid
        Long userId = JwtUtil.parseUser().getUserId();
        //获得可以得到的物资属性
        List<ActionConfigVo> actionConfigVoList = actionConfigMapper.getActionConfig(actionConfigQo.getActionConfigId());

            for (ActionConfigVo actionConfigVo :actionConfigVoList) {
                //类型为1说明是获得
                if (actionConfigVo.getActionConfigType()==1){
                    //获得的数量
                    Integer amount = actionConfigVo.getAmount();

                    LambdaQueryWrapper<UserMaterials> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                    //查询之前有多少物资
                    UserMaterials userMaterialsVo = userMaterialsMapper.selectOne(lambdaQueryWrapper.eq(UserMaterials::getUserId, userId).eq(UserMaterials::getMaterialsId, actionConfigVo.getMaterialsId()));
                    if (userMaterialsVo==null){

                        UserMaterials userMaterials = UserMaterials.builder().userId(userId).stock(Long.valueOf(amount)).materialsId(actionConfigVo.getMaterialsId()).build();

                        userMaterialsMapper.insert(userMaterials);

                    }else{
                        //修改库存
                        UserMaterials userMaterials = UserMaterials.builder().materialsId(actionConfigVo.getMaterialsId()).userId(userId).stock(Long.valueOf(amount)).build();

                        userMaterialsMapper.updataUserResources(userMaterials);
                    }
                } else if (actionConfigVo.getActionConfigType()==2) {
                    //获得消耗数量
                    Integer amount = actionConfigVo.getAmount();
                    LambdaQueryWrapper<UserMaterials> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                    //查询之前有多少物资
                    UserMaterials userMaterialsVo = userMaterialsMapper.selectOne(lambdaQueryWrapper.eq(UserMaterials::getUserId, userId).eq(UserMaterials::getMaterialsId, actionConfigVo.getMaterialsId()));

                    if (userMaterialsVo==null){

                      throw new RuntimeException(actionConfigVo.getMaterialsName()+"数量不足");

                    }else if (userMaterialsVo.getStock() < amount){

                        throw new RuntimeException(actionConfigVo.getMaterialsName()+"数量不足");

                    }

                    //修改库存
                    UserMaterials userMaterials = UserMaterials.builder().materialsId(actionConfigVo.getMaterialsId()).userId(userId).stock(Long.valueOf(amount)).build();

                    userMaterialsMapper.updataUserResourcesLose(userMaterials);


                }
            }

        UserTasks userTasks = UserTasks.builder().build();

        userTasks.setUserId(userId);
        userTasks.setTaskId(actionConfigQo.getActionConfigId());
        userTasks.setStartedAt(now);
        userTasks.setLastProcessedAt(now);

        LambdaQueryWrapper<UserTasks> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        lambdaQueryWrapper.eq(UserTasks::getUserId,userId);
        lambdaQueryWrapper.eq(UserTasks::getTaskId,actionConfigQo.getActionConfigId());

        userTasksMapper.update(userTasks,lambdaQueryWrapper);

        return actionConfigVoList;
    }

    @Override
    public List<ActionConfigVo> getroleOccupation(ActionConfigQo actionConfigQo) {
        //获得userid
        Long userId = JwtUtil.parseUser().getUserId();

        DogCharacter dogCharacterVo = dogCharacterMapper.selectById(actionConfigQo.getDogCharacterId());

        if (dogCharacterVo.isRemove()&&dogCharacterVo.getActionConfigId()!=null && !Objects.equals(dogCharacterVo.getActionConfigId(), actionConfigQo.getActionConfigId())){
            throw new RuntimeException("该狗狗已经被使用");
        }

        ActionConfig actionConfig = actionConfigMapper.selectById(actionConfigQo.getActionConfigId());

        //获得可以得到的物资属性
        List<ActionConfigVo> actionConfigVoList = actionConfigMapper.getActionConfig(actionConfigQo.getActionConfigId());

        UserTasksQo userTasksQo = UserTasksQo.builder().build();

        userTasksQo.setTaskId(actionConfigQo.getActionConfigId());

        userTasksQo.setUserId(userId);

        userTasksQo.setCharacterId(actionConfigQo.getDogCharacterId());

        userTasksService.saveUserTasks(userTasksQo);

        dogCharacterVo.setRemove(true);

        dogCharacterVo.setActionConfigId(actionConfigQo.getActionConfigId());

        dogCharacterMapper.updateById(dogCharacterVo);

        return actionConfigVoList;
    }

    @Override
    public List<ActionConfigVo> getActionConfigTable() {

        List<ActionConfigVo> actionConfigVos = actionConfigMapper.selectList(new LambdaQueryWrapper<ActionConfig>().eq(ActionConfig::getRemove,false)).stream().map(actionConfig -> {

            ActionConfigVo actionConfigVo = ActionConfigVo.builder().build();
            BeanUtils.copyProperties(actionConfig, actionConfigVo);

            return actionConfigVo;
        }).collect(Collectors.toList());

        return actionConfigVos;
    }

    @Override
    public void upDataActionConfigTable(ActionConfigUpdataQo actionConfigUpdataQo) {

        ActionConfig actionConfig = ActionConfig.builder().build();

        BeanUtils.copyProperties(actionConfigUpdataQo,actionConfig);

        if (actionConfig.getActionConfigId()!=null){

            actionConfigMapper.updateById(actionConfig);

        }else{

            actionConfigMapper.insert(actionConfig);

        }


    }

    @Override
    public void deleteActionConfigTable(ActionConfigUpdataQo actionConfigUpdataQo) {

        ActionConfig actionConfig = ActionConfig.builder().actionConfigId(actionConfigUpdataQo.getActionConfigId()).remove(true).build();

        actionConfigMapper.updateById(actionConfig);

    }
}
